<?php
namespace app\common\loglic;

use app\common\loglic\Model;

class Menu extends Model
{
    //初始查询参数
    private $args = [
        'where' => [],
        'cache' => false,
        'order' => [
            'menu_weight' => 'desc',
            'menu_id' => 'desc',
        ],
    ];

    //修改器（写入数据库前）
    private function dataSet($data=[])
    {
        return ['menu'=>$data];
    }

    //获取器（查询数据库后）
    private function dataGet($data=[])
    {
        return $data;
    }

    /**
     * 添加
     * @version 1.0.0 首次引入
     * @param array $data 必需;数组格式;默认:空
     * @return int 返回自增ID或0
     */
    public function dataSave($data=[])
    {
        //数据初始化
        unset($data['menu_id']);
        //数据修改器
        $data = $this->dataSet($data);
        //表单验证规则
        $this->validateRule = [
            'menu_name' => 'require',
            'menu_url' => 'require',
        ];
        $this->validateMsg = [
            'menu_name.require' => '请填写菜单名称',
            'menu_url.unique' => '请填写菜单链接',
        ];
        if(!$this->validateData($data['menu'])){
            return false;
        }
        //写入数据库
        return $this->modelSave('common/Menu',$data);
    }

    //修改
    public function dataUpdate($data=[])
    {
        //数据修改器
        $data = $this->dataSet($data);
        //表单验证规则
        $this->validateRule = [
            'menu_id' => 'number',
            'menu_name' => 'require',
            'menu_url' => 'require',
        ];
        $this->validateMsg = [
            'menu_id.number' => 'ID错误',
            'menu_name.require' => '请填写菜单名称',
            'menu_url.unique' => '请填写菜单链接',
        ];
        //表单验证未通过
        if(!$this->validateData($data['menu'])){
            return false;
        }
        //更新条件
        $where = [
            'menu_id' => $data['menu']['menu_id'],
        ];
        return $this->modelUpdate('common/Menu', $where, $data);
    }

    //按条件分类列表
    public function all($args=[])
    {
        //合并参数
        $args = array_merge($this->args,$args);
        //查询数据
        return $this->modelSelect('common/Menu', $args);
    }

    //按条件获取树状列表
    public function allTree($args=[])
    {
        $result = $this->all($args);
        if($result){
            $result = model('common/Tree','loglic')->list_to_tree($result,'menu_id','menu_parent');
        }
        return $result;
    }

    //按条件获取层级列表
    public function allLevel($args=[])
    {
        $result = $this->allTree($args);
        if($result){
            $result = model('common/Tree','loglic')->tree_to_level($result,'menu_name');
        }
        return $result;
    }

    //按主键ID删除
    public function deleteId($pkId=0)
    {
        if(!$pkId){
            return 0;
        }
        return $this->modelDelete('common/Menu', ['menu_id'=>$pkId]);
    }

    //权重值增减
    public function updateWeight($pkId=0, $value=0)
    {
        if(!$pkId || !$value){
            return false;
        }
        if($value>0){
            return model('common/Menu')->where('menu_id',$pkId)->setInc('menu_weight', abs($value));
        }else{
            return model('common/Menu')->where('menu_id',$pkId)->setDec('menu_weight', abs($value));
        }
    }

    //修改显示状态
    public function updateStatus($pkId=[], $value='normal')
    {
        if(!$pkId){
            return false;
        }
        return db('menu')->where('menu_id','in',$pkId)->setField('menu_status', $value);
    }

    //修改分组数字
    public function updateGroup($pkId=[], $value=0)
    {
        if(!$pkId){
            return false;
        }
        return db('menu')->where('menu_id','in',$pkId)->setField('menu_group', $value);
    }

    //按ID查询单个数据
    public function getId($id=0, $cache=false, $status=false)
    {
        $where = [
            ['menu_id','eq',$id],
        ];
        if($status){
            array_push($where,['menu_status','eq',$status]);
        }
        $result = $this->modelGet('common/Menu',[
            'cache' => $cache,
            'where' => $where,
        ]);
        return $this->dataGet($result);
    }

    //获取字段最大值
    public function getWeightMax()
    {
        return $this->dbMax('menu','menu_weight')+1;
    }

    //返回N条列结果键名为ID键值为Name
    public function option($limt=10)
    {
        return db('menu')->limit($limt)->where([
            ['menu_status','eq','normal'],
        ])->order([
            'menu_weight' => 'desc',
            'menu_id' => 'desc',
        ])->column('menu_name','menu_id');
    }

    //所回层级列表选项
    public function optionLevel($limt=0)
    {
        $result = db('menu')->field('menu_id,menu_name,menu_parent')->limit($limt)->where([
            ['menu_status','eq','normal'],
        ])->order([
            'menu_weight' => 'desc',
            'menu_id' => 'desc',
        ])->select();
        if($result){
            $result = model('common/Tree','loglic')->list_to_tree($result,'menu_id','menu_parent');
            $result = model('common/Tree','loglic')->tree_to_level($result,'menu_name');
        }
        $result = array_column($result,'menu_name','menu_id');
        $result[0] = '------';
        return $result;
    }

    //简单调用数据列表，返回树状结构
    public function items($group=1,$limit=10,$sortOrder=['menu_weight'=>'desc','menu_id'=>'desc'],$field='*'){
        $result = $this->dbSelect('menu', [
            'field'  => $field,
            'limit'  => $limit,
            'cache'  => config('cache.expire_items'),
            'order'  => $sortOrder,
            'where'  => [
                ['menu_group','in',$group],
                ['menu_status','eq','normal'],
            ],
        ]);
        if($result){
            $result = model('common/Tree','loglic')->list_to_tree($result,'menu_id','menu_parent');
        }
        return $result;
    }
}